#include "name_dialog.h"

//(*InternalHeaders(NameDialog)
#include <wx/sizer.h>
#include <wx/stattext.h>
#include <wx/textctrl.h>
#include <wx/intl.h>
#include <wx/button.h>
#include <wx/string.h>
//*)

//(*IdInit(NameDialog)
const long NameDialog::ID_TEXTCTRL1 = wxNewId();
const long NameDialog::ID_TEXTCTRL2 = wxNewId();
const long NameDialog::ID_TEXTCTRL3 = wxNewId();
//*)

BEGIN_EVENT_TABLE(NameDialog,wxDialog)
	//(*EventTable(NameDialog)
	//*)
END_EVENT_TABLE()

NameDialog::NameDialog(wxWindow* parent,
                       wxString* result_keyword, wxString* result_sortname, wxString* result_hints)
{
    this->result_keyword = result_keyword;
    this->result_sortname = result_sortname;
    this->result_hints = result_hints;

	//(*Initialize(NameDialog)
	wxButton* ok_button;
	wxStaticText* StaticText2;
	wxStaticText* StaticText1;
	wxStaticText* StaticText3;
	wxBoxSizer* button_sizer;
	wxFlexGridSizer* grid_sizer;
	wxBoxSizer* top_sizer;

	Create(parent, wxID_ANY, _("Enter name"), wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE|wxRESIZE_BORDER, _T("wxID_ANY"));
	top_sizer = new wxBoxSizer(wxVERTICAL);
	grid_sizer = new wxFlexGridSizer(0, 2, 0, 0);
	grid_sizer->AddGrowableCol(1);
	StaticText1 = new wxStaticText(this, wxID_ANY, _("&First and middle names"), wxDefaultPosition, wxDefaultSize, 0, _T("wxID_ANY"));
	grid_sizer->Add(StaticText1, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	first_box = new wxTextCtrl(this, ID_TEXTCTRL1, wxEmptyString, wxDefaultPosition, wxSize(200,-1), 0, wxDefaultValidator, _T("ID_TEXTCTRL1"));
	first_box->SetFocus();
	grid_sizer->Add(first_box, 1, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	StaticText2 = new wxStaticText(this, wxID_ANY, _("&Last name"), wxDefaultPosition, wxDefaultSize, 0, _T("wxID_ANY"));
	grid_sizer->Add(StaticText2, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	last_box = new wxTextCtrl(this, ID_TEXTCTRL2, wxEmptyString, wxDefaultPosition, wxSize(200,-1), 0, wxDefaultValidator, _T("ID_TEXTCTRL2"));
	grid_sizer->Add(last_box, 1, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	StaticText3 = new wxStaticText(this, wxID_ANY, _("&Second last name"), wxDefaultPosition, wxDefaultSize, 0, _T("wxID_ANY"));
	grid_sizer->Add(StaticText3, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	last2_box = new wxTextCtrl(this, ID_TEXTCTRL3, wxEmptyString, wxDefaultPosition, wxSize(200,-1), 0, wxDefaultValidator, _T("ID_TEXTCTRL3"));
	grid_sizer->Add(last2_box, 1, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	top_sizer->Add(grid_sizer, 1, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	button_sizer = new wxBoxSizer(wxHORIZONTAL);
	button_sizer->Add(-1,-1,1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	ok_button = new wxButton(this, wxID_OK, _("&OK"), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("wxID_OK"));
	ok_button->SetDefault();
	button_sizer->Add(ok_button, 0, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	Button2 = new wxButton(this, wxID_CANCEL, _("&Cancel"), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("wxID_CANCEL"));
	button_sizer->Add(Button2, 0, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	top_sizer->Add(button_sizer, 0, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	SetSizer(top_sizer);
	top_sizer->Fit(this);
	top_sizer->SetSizeHints(this);

	Connect(wxID_OK,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&NameDialog::OnOK);
	Connect(wxID_CANCEL,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&NameDialog::OnCancel);
	//*)

}

NameDialog::~NameDialog()
{
	//(*Destroy(NameDialog)
	//*)
}

static void NameHintVariants(wxString& hints, const wxString& first, const wxString& last)
{
//  Valasek, Danecek
    static struct {const wchar_t *name1, *name2;} first_name_aliases[] = {
        {L"Anton",      L"Antonin"      },
        {L"Gustav",     L"Augustin"     },
        {L"Gustav",     L"August"       },
        {L"Augustin",   L"August"       },
        {L"Augustin",   L"Gustav"       },
        {L"Pavol",      L"Pavel"        },
        {L"Zdenko",     L"Zdeno"        }
    };

    static struct {const wchar_t *name1, *name2;} first_name_nicks[] = {
        {L"Alexander",  L"Alex"     },
        {L"Alexander",  L"Sano"     },
        {L"Anton",      L"Tono"     },
        {L"Anton",      L"Tonda"    },
        {L"Anton",      L"Tondo"    },
        {L"Bohuslav",   L"Bohus",   },
        {L"Branislav",  L"Brano",   },
        {L"Eduard",     L"Edo"      },
        {L"Edmund",     L"Edo"      },
        {L"Ferdinand",  L"Fero"     },
        {L"Frantisek",  L"Fero"     },
        {L"Frantisek",  L"Franta"   },
        {L"Gustav",     L"Gusto"    },
        {L"Imrich",     L"Imro"     },
        {L"Jozef",      L"Jozo"     },
        {L"Ladislav",   L"Laco"     },
        {L"Matej",      L"Mato"     },
        {L"Martin",     L"Mato"     },
        {L"Michal",     L"Miso"     },
        {L"Miroslav",   L"Miro"     },
        {L"Otto",       L"Oto"      },
        {L"Pavol",      L"Palo"     },
        {L"Samuel",     L"Samo"     },
        {L"Zuzana",     L"Zuza"     }
    };

    if (!hints.IsEmpty()) hints.Append(wxT(", "));
    hints.Append(                  last + wxT(", "));
    hints.Append(first.Mid(0,1) + wxT(". ") + last + wxT(", "));
    hints.Append(first    + wxT(" ")  + last);


}

void NameDialog::OnOK(wxCommandEvent& event)
{
    wxString first = first_box->GetValue();
    wxString last1  = last_box->GetValue();
    wxString last2 = last2_box->GetValue();

    if (first.IsEmpty() || last1.IsEmpty())
        return;

    wxString last = last1;
    if (!last2.IsEmpty()) {
        last += '-';
        last += last2;
    }

    *result_keyword = first + wxT(" ") + last;
    *result_sortname = last + wxT(", ") + first;
    result_hints->Clear();

    // Viera
    // Ursiny, Hagara, Thurzo, Smidke
    wxString first_hint=first, last1_hint=last1, last2_hint=last2;
    first.EndsWith(wxT("o"), &first_hint) || first.EndsWith(wxT("a"), &first_hint);
    last1.EndsWith(wxT("y"), &last1_hint) || last1.EndsWith(wxT("a"), &last1_hint)
    || last1.EndsWith(wxT("o"), &last1_hint) || last1.EndsWith(wxT("e"), &last1_hint);
    last2.EndsWith(wxT("y"), &last2_hint) || last2.EndsWith(wxT("a"), &last2_hint)
    || last2.EndsWith(wxT("o"), &last2_hint) || last2.EndsWith(wxT("e"), &last2_hint);

    first_hint.Append('*');
    last1_hint.Append('*');
    last2_hint.Append('*');

    NameHintVariants(*result_hints, first_hint, last1_hint);
    if (!last2.IsEmpty()) {
        NameHintVariants(*result_hints, first_hint, last2_hint);
        NameHintVariants(*result_hints, first_hint, last1_hint + wxT("-") + last2_hint);
    }

    EndModal(wxID_OK);
}

void NameDialog::OnCancel(wxCommandEvent& event)
{
    EndModal(wxID_CANCEL);
}
